package task

import (
	"github.com/beego/beego/v2/adapter/toolbox"
	"github.com/prometheus/common/log"
	"remote_cmd/models"
)

func init() {
	task := toolbox.NewTask("CloseClient", "*/50 */30 * * * *", CloseClient)
	toolbox.AddTask("CloseClient", task)
}

// CloseClient TODO 并发问题待解决
func CloseClient() error {
	log.Info("Began to close the client timeout")
	for ip, linux := range models.LinuxCache {
		isClose, err := linux.Timeout()
		if err != nil {
			log.Error("close client[%s] error: %v", ip, err)
			continue
		}
		if isClose {
			delete(models.LinuxCache, ip)
		}
	}
	return nil
}
